package com.example.webcrawler.controller;

import com.example.webcrawler.model.ApiRequest;
import com.example.webcrawler.model.FlowTest;
import com.example.webcrawler.service.mpl.DataStorageService;
import com.example.webcrawler.service.FlowService;
import com.example.webcrawler.service.RequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/requests")
public class ApiController {

    @Autowired
    private RequestService requestService;

    @Autowired
    private FlowService flowService;

    @Autowired
    private DataStorageService dataStorageService;

    /**
     * 保存请求
     */
    @PostMapping("/save")
    public Map<String, Object> saveRequest(@RequestBody ApiRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            ApiRequest savedRequest = requestService.saveRequest(request);
            result.put("success", true);
            result.put("request", savedRequest);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "保存请求失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 获取所有已保存的请求
     */
    @GetMapping("/saved")
    public Map<String, Object> getSavedRequests() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ApiRequest> requests = requestService.getAllSavedRequests();
            result.put("success", true);
            result.put("requests", requests);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取请求失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 更新请求
     */
    @PutMapping("/update/{requestId}")
    public Map<String, Object> updateRequest(@PathVariable String requestId, @RequestBody ApiRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            ApiRequest updatedRequest = requestService.updateRequest(requestId, request);
            result.put("success", true);
            result.put("request", updatedRequest);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新请求失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 删除请求
     */
    @DeleteMapping("/delete/{requestId}")
    public Map<String, Object> deleteRequest(@PathVariable String requestId) {
        Map<String, Object> result = new HashMap<>();
        try {
            requestService.deleteRequest(requestId);
            result.put("success", true);
            result.put("message", "请求已删除");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除请求失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 保存流程
     */
    @PostMapping("/flow/save")
    public Map<String, Object> saveFlow(@RequestBody FlowTest flowTest) {
        Map<String, Object> result = new HashMap<>();
        try {
            FlowTest savedFlow = flowService.saveFlow(flowTest);
            result.put("success", true);
            result.put("flow", savedFlow);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "保存流程失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 获取所有已保存的流程
     */
    @GetMapping("/flow/saved")
    public Map<String, Object> getSavedFlows() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<FlowTest> flows = flowService.getAllSavedFlows();
            result.put("success", true);
            result.put("flows", flows);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取流程失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 删除流程
     */
    @DeleteMapping("/flow/delete/{flowId}")
    public Map<String, Object> deleteFlow(@PathVariable String flowId) {
        Map<String, Object> result = new HashMap<>();
        try {
            flowService.deleteFlow(flowId);
            result.put("success", true);
            result.put("message", "流程已删除");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除流程失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 执行流程
     */
    @PostMapping("/flow/execute")
    public Map<String, Object> executeFlow(@RequestBody FlowTest flowTest) {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> executionResult = flowService.executeFlow(flowTest);
            result.put("success", true);
            result.put("data", executionResult);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "执行流程失败: " + e.getMessage());
        }
        return result;
    }
}
